<?php

namespace app\data\model;

use think\admin\Model;
use think\facade\Db;

class ActActivity extends Model
{
    protected $table = 'act_activity';


    public function getSaleStatusAttr()
    {
        $now = time();
        $begin=strtotime($this->begin_time);
        $end = strtotime($this->end_time);
        if($now<$begin){
            return 4;
        }elseif($now>$end){
            return 2;
        }else{
            if($this->rest=0){
                return 3;
            }else{
                return 1;
            }
        }

    }

    public function articleModel()
    {
        return $this->hasOne(ActArticleModel::class,  'activity_id');
    }

    public function userModel()
    {
        return $this->belongsTo(DataUser::class,  'create_user_id');
    }

    public function ticketModel()
    {
        return $this->hasOne(ActActivityTicket::class,'activity_id');
    }

    public function OrderModel()
    {
        return $this->hasOne(ActOrder::class,'activity_id');
    }

    public function themeModel()
    {
        return $this->hasMany(ActActivityTheme::class, 'activity_id');
    }

    public function guestModel()
    {
        return $this->hasMany(ActActivityGuest::class, 'activity_id');
    }

    public function getTypeNameAttr()
    {
        return ActSettingType::where('id',$this->type_id)->value('name');
    }

    public function getRegionNameAttr()
    {
        $region = ActRegion::where('id',$this->region_id)->findOrFail();
        $name_arr = $region->getFullNameArr();

        return implode(' ',$name_arr);
    }

    public function getCreatorNameAttr()
    {
        return ActUserModel::where('id',$this->create_user_id)->value('name');
    }


    public function getRemainTicketAttr(){
        //剩余票
        $remain_ticket = ActTicketDetails::where('activity_id',$this->id)->sum('rest_quantity');
        //剩余分组票
        $remian_goup_ticket = ActTicketGroup::where('activity_id',$this->id)->sum('group_remain_number');
        return $remain_ticket + $remian_goup_ticket;
    }

    public function getTicketCountAttr(){
        //剩余票
        $remain_ticket = ActTicketDetails::where('activity_id',$this->id)->sum('quantity');
        //剩余分组票
        $remian_goup_ticket = ActTicketGroup::where('activity_id',$this->id)->sum('group_number');
        return $remain_ticket + $remian_goup_ticket;
    }

    public function getStatusNameAttr()
    {
        $now = time();
        $begin=strtotime($this->begin_time);
        $end = strtotime($this->end_time);
        if($now<$begin){
            return '未开始';
        }elseif($now>$end){
            return '已结束';
        }else{
            return '进行中';
        }
    }

    public function getStateAttr($value,$data){
        $status = [0=>'待审核',1=>'已发布',2=>'审核未通过',3=>'已下架'];
        return $status[$data['status']];
    } 

    public function saveActivityGuests(array $guests)
    {
        ActActivityGuest::where('activity_id',$this->id)->delete();
        foreach ($guests as $guest) {
            ActActivityGuest::create([
                'activity_id' => $this->id,
                'profile' => $guest['profile'],
                'username' => $guest['username'],
                'posit' => $guest['posit'],
                'company_name' => $guest['company_name'],
                'user_desc' => $guest['user_desc'],
            ]);
        }
    }

    public function saveActivityTheme(array $themeIdArr)
    {
        ActActivityTheme::where('activity_id',$this->id)->delete();
        foreach ($themeIdArr as $theme_id) {
            ActActivityTheme::create([
                'activity_id' => $this->id,
                'theme_id' => $theme_id,
            ]);
        }

    }

    public function saveActivityArticle()
    {
        $articleModel = $this->articleModel()->findOrEmpty();
        $articleModel->title = $this->getAttr('name');
        $articleModel->subtitle=$this->organizer_name;
        $articleModel->logo=$this->logo;
        $articleModel->content = $this->makeArticleHtml();
        $articleModel->activity_id = $this->id;
        $articleModel->begin_time = $this->begin_time;
        $articleModel->end_time = $this->end_time;
        $articleModel->cat_id = 1;
        $articleModel->save();
    }

    public function makeArticleHtml()
    {
        //todo 生成文章html
        return "<p>{$this->desc}</p>";
    }

    public function saveActivityTicket($ticket)
    {
        
        //当前但张门票
        $ticketModel = ActActivityTicket::where('activity_id',$this->id)->findOrEmpty();
        $ticketModel->desc                      = $ticket['desc'];
        // $ticketModel->price                     = $ticket['price'];
        // $ticketModel->stock                     = $ticket['stock'];
        // $ticketModel->rest                      = $ticket['stock'];
        $ticketModel->sale_begin                = $ticket['sale_begin'];
        $ticketModel->sale_end                  = $ticket['sale_end'];
        $ticketModel->sale_begin_timestamp      = strtotime($ticket['sale_begin']);
        $ticketModel->sale_end_timestamp        = strtotime($ticket['sale_end']);;
        $ticketModel->activity_id               = $this->id;
        $ticketModel->ticket_yx_start_timestamp = strtotime($ticket['ticket_yx_start']);
        $ticketModel->sale_end_timestamp        = strtotime($ticket['ticket_yx_end']);
        $ticketModel->ticket_yx_start           = $ticket['ticket_yx_start'];
        $ticketModel->ticket_yx_end             =  $ticket['ticket_yx_end'];
        $ticketModel->is_open                   =  $ticket['is_open'];
        $ticketModel->is_verify                 =  $ticket['is_verify'];
        $ticketModel->ticket_min                =  $ticket['ticket_min'];
        $ticketModel->ticket_max                =  $ticket['ticket_max'];
        $ticketModel->save();
        $ticket_details                         = $ticket['tickets'];
        if(count($ticket_details)){
            ActTicketDetails::where('activity_id',$this->id)->delete();
            foreach ($ticket_details as $key => $value) {
                ActTicketDetails::create([
                    'activity_id' => $this->id,
                    'ticket_id'   => $ticketModel->id,
                    'name'        => $value['name'],
                    'quantity'    => $value['quantity'],
                    'rest_quantity' => $value['quantity'],
                    'price'        => $value['price'],
                ]);
            }
        } 
    }

    /**
     * @name:分组保存
     * @author:Sam
     * @date:2024-04-10
     * @param:[string] param [desc]
    */
    public function saveActivityGroup($group=[]){
        if(count($group)){
            ActTicketGroup::where('activity_id',$this->id)->delete();
            foreach ($group as $key => $value) {
                $ticket = ActTicketDetails::where('name',$value['group_ticket'])->find();
                if(!$ticket) throw new \think\Exception('票券未找到');
                if($ticket->rest_quantity < $value['group_number']) throw new \think\Exception('余票不足');
                $ticket->rest_quantity -= $value['group_number'];
                $ticket->save();
                ActTicketGroup::create([
                    'activity_id'          => $this->id,
                    'group_name'           => $value['group_name'],
                    'group_number'         => $value['group_number'],
                    'group_ticket'         => $value['group_ticket'],
                    'group_price'          => $value['group_price'],
                    'group_sale_begin'     => $value['group_sale_begin'],
                    'group_sale_begin'     => $value['group_sale_begin'],
                    'group_sale_end'       => $value['group_sale_end'],
                    'group_yx_start_time'  => $value['group_yx_start_time'],
                    'group_yx_end_time'    => $value['group_yx_end_time'],
                    'group_is_open'        => $value['group_is_open'],
                    'group_is_verify'      => $value['group_is_verify'],
                    'group_remain_number'  => $value['group_number'],
                    'ticket_id'            => $ticket->id,
                ]);
            }
        }
    }

    /**
     * @name:diy
     * @author:Sam
     * @date:2024-04-10
     * @param:[string] param [desc]
    */
    public function saveActivityDiy($diy = []){
        if(count($diy)){
            ActActivityDiy::where('activity_id',$this->id)->delete();
            foreach ($diy as $key => $value) {
                ActActivityDiy::create([
                    'activity_id' => $this->id,
                    'label'       => $value['label'],
                    'must'        => $value['label'],
                    'label_type'  => $value['type'],
                    'tips'        => $value['tips'],
                    'desc'        => $value['desc'],
                    'options'     => $value['options']
                ]);
            }
        }
    }


}